;This file is part of C45IDL.
;
;C45IDL is free software: you can redistribute it and/or modify
;it under the terms of the GNU Lesser General Public License as published by
;the Free Software Foundation, either version 3 of the License, or
;(at your option) any later version.
;
;C45IDL is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU Lesser General Public License for more details.
;
;You should have received a copy of the GNU Lesser General Public License
;along with C45IDL.  If not, see <http://www.gnu.org/licenses/>. 


FUNCTION openFile, title, filter
    file = DIALOG_PICKFILE(TITLE=title,FILTER = filter)
    ;if file doesn't exists, return 0
    if ~ FILE_TEST(file) then begin
      return, 0
    endif else return, file
END

PRO addText, widgetId, msg
  ;WIDGET_CONTROL,widgetId,get_value=curText
  ;newText=curText+'\n '+msg
  WIDGET_CONTROL,widgetId,set_value=newText
END

PRO writeMsg, text
  common DTC, output
  if ~ n_elements(output) then begin
    print, text    
  endif else begin
    ;WIDGET_CONTROL,output,get_value=curText
    WIDGET_CONTROL,output,set_value=text,/APPEND;+string([13B, 10B])
  endelse
END


PRO dtcui
;create gui
parent = WIDGET_BASE(TITLE='Decision Tree C45 classifier', /FRAME)
;button
;filelist=widget_text(parent,xsize=18,ysize=5,/scroll,/editable,/all_events,yoffset =28,uvalue='filelist')
;titletext=widget_text(parent,xsize=10,/editable,/all_events,yoffset=180,uvalue='titletext')
x1=0
x2=250
x3=400
y=0
inc=30
res=WIDGET_LABEL(parent, value="ROI samples' file:", xoffset=x1,yoffset=y+5)
samplesFileText=WIDGET_TEXT(parent, value="", uvalue='SamplesFile', xoffset=x2,yoffset=y,/EDITABLE)
res=WIDGET_BUTTON(parent, /FRAME, VALUE='Open',UVALUE='openRoi', xoffset=x3,yoffset=y)
y+=inc
res=WIDGET_LABEL(parent, value="Training samples percentage:", xoffset=x1,yoffset=y+5)
samplesPercentText=WIDGET_TEXT(parent, value='0.7', uvalue='SamplesPercent', xoffset=x2,yoffset=y,/EDITABLE)
y+=inc
res=WIDGET_LABEL(parent, value="Image to classify:", xoffset=x1,yoffset=y+5)
imageFileText=WIDGET_TEXT(parent, value="", uvalue='imageFile', xoffset=x2,yoffset=y,/EDITABLE)
res=WIDGET_BUTTON(parent, /FRAME, VALUE='Open',UVALUE='openImage', xoffset=x3,yoffset=y)
y+=inc
res=WIDGET_LABEL(parent, value='Minimum samples per node',xoffset=x1,yoffset=y+5)
minSamplesText=WIDGET_TEXT(parent, value='25', uvalue='MinNumSamples', xoffset=x2,yoffset=y,/EDITABLE)
y+=inc
res=WIDGET_LABEL(parent, value='Maximum percentual error in a node',xoffset=x1,yoffset=y+5)
maxErrorText=WIDGET_TEXT(parent, value='0.01', uvalue='MaxNodeError', xoffset=x2,yoffset=y,/EDITABLE)
y+=inc

y+=inc
res=WIDGET_LABEL(parent, value="Output tree file:", xoffset=x1,yoffset=y+5)
treeFileText=WIDGET_TEXT(parent, value="", uvalue='treeFile', xoffset=x2,yoffset=y,/EDITABLE)
res=WIDGET_BUTTON(parent, /FRAME, VALUE='Open',UVALUE='chooseTreeImage', xoffset=x3,yoffset=y)
y+=inc

res=WIDGET_LABEL(parent, value="Output classification image:", xoffset=x1,yoffset=y+5)
classImageText=WIDGET_TEXT(parent, value="classes.tif", uvalue='classFile', xoffset=x2,yoffset=y,/EDITABLE)
res=WIDGET_BUTTON(parent, /FRAME, VALUE='Open',UVALUE='chooseClassImage', xoffset=x3,yoffset=y)
y+=inc

res= WIDGET_BUTTON(parent, /FRAME, VALUE='Build tree',UVALUE='build', xoffset=(x1+x3)/2,yoffset=y)
y+=inc

common DTC, output
output=WIDGET_TEXT(parent, value="", uvalue='msgText', xoffset=x1,yoffset=y,YSIZE=5, XSIZE=70,/WRAP, /SCROLL)



;***Realize the menu
WIDGET_CONTROL, parent, /REALIZE

;*** Wait for the first event: ***
event=WIDGET_EVENT(parent)

while 1 do begin
  ;*** Determine which button sent the event:***
  WIDGET_CONTROL,event.id,GET_UVALUE=uval
  CASE uval OF
  
  'openRoi': BEGIN
    file= DIALOG_PICKFILE(TITLE="Open ROI text file",FILTER = "*.txt")
    WIDGET_CONTROL,samplesFileText,set_value=file
  END
  
  'chooseClassImage': BEGIN
    file=DIALOG_PICKFILE(TITLE="Choose output classification image name",FILTER = "*.tif")
    WIDGET_CONTROL,classImageText,set_value=file
  END
  
  'openImage': BEGIN
    file=DIALOG_PICKFILE(TITLE="Open image to classify",FILTER = "*.tif")
    WIDGET_CONTROL,imageFileText,set_value=file
  END
  
  'chooseTreeImage': BEGIN
    file=DIALOG_PICKFILE(TITLE="Choose output tree file",FILTER = "*.txt")
    WIDGET_CONTROL,treeFileText,set_value=file
  END
  
  
  
  'build':BEGIN
    ;*** Destroy the GUI, let caller program proceed: ***
    WIDGET_CONTROL, samplesFileText,get_value=samplesFile
    WIDGET_CONTROL, imageFileText,get_value=imageFile
    WIDGET_CONTROL, classImageText,get_value=classFile
    WIDGET_CONTROL, minSamplesText,get_value=minSamples
    WIDGET_CONTROL, maxErrorText,get_value=maxError
    WIDGET_CONTROL, samplesPercentText,get_value=percent
    WIDGET_CONTROL, treeFileText,get_value=treeFile
    percent=(FLOAT(percent))[0]
    minSamples=(FIX(minSamples))[0]
    dtcClassifier,samplesFile,imageFile, percent, minSamples, maxError, classFile, treeFile
    ;res=DIALOG_MESSAGE('Process finished.',TITLE='Classification')
    ;WIDGET_CONTROL, parent, /DESTROY
    BREAk
  END
  
  
  ENDCASE
  
  ;*** Wait for the next event: ***
  event=WIDGET_EVENT(parent)
endwhile

;*** Destroy the GUI, let caller program proceed: ***
;WIDGET_CONTROL, parent, /DESTROY
end